package com.cskt.petproject178.controller;

import com.cskt.petproject178.pojo.vo.ResultVO;
import com.cskt.petproject178.service.third.WeChatPayService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信支付控制器
 *
 * @author 27013
 **/

@RestController
@RequestMapping(value = "/oms/pay/wechat")
public class WeChatPayController {
    private static  final Logger log= LoggerFactory.getLogger(WeChatPayController.class);
@Resource
private WeChatPayService weChatPayService;

    @PostMapping(value = "/notify")
    public void weChatPayNotify(HttpServletRequest request) {
        //必须从request中获取回调相关数据
        try {
            String body = new String(request.getInputStream().readAllBytes());
            String signature = request.getHeader("Wechatpay-Signature");
            String serialNumber = request.getHeader("Wechatpay-Serial");
            String nonce = request.getHeader("Wechatpay-Nonce");
            String timestamp = request.getHeader("Wechatpay-Timestamp");
            String signatureType = request.getHeader("Wechatpay-Signature-Type");
             weChatPayService.wechatPayCallback(body,serialNumber,signature,signatureType,nonce,timestamp);
        } catch (IOException e) {
          log.error("系统异常，",e);
        }
    }

    @GetMapping(value = "/codeUrl/{orderNo}")
    public ResultVO<?> codeUrl(@PathVariable String orderNo){
        String codeUrl = weChatPayService.codeUrl(orderNo);
        if (StringUtils.hasText(codeUrl)) {
            Map<String,String> resultMap=new HashMap<>();
            resultMap.put("codeUrl",codeUrl);
            return ResultVO.OK(resultMap);
        }
        return ResultVO.ERR();
    }
}

